extends Node
class_name 状态机

@export var starting_state: BaseState

var states: Array[BaseState]
var current_state: BaseState

# 状态机是否启动
var is_launched: bool = false

func _ready() -> void:
	for state:BaseState in get_children():
		states.append(state)
 
# 启动状态机
func launch():
	is_launched = true
	current_state = starting_state
	current_state.on_state_enter()
	
# 切换状态	
func change_state(name):
	var new_state = get_state(name)
	if new_state != null:
		current_state.on_state_exit()
		current_state = new_state
		current_state.on_state_enter()
	
	pass
	
func get_state(name:String):
	for s in states:
		if s.state_name == name:
			return s
	
	return null			
	
	
func _process(delta: float) -> void:
	if is_launched:
		current_state.on_state_process(delta)

func _physics_process(delta: float) -> void:
	if is_launched:
		current_state.on_state_physics_process(delta)
	
		
		
